<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="店员昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入店员昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="queryParams.sex"
          placeholder="请选择性别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_SEX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
	  
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
	<el-tabs v-model="queryParams.status" type="card" @tab-click="handleTabClick">
	  <el-tab-pane
	    v-for="item in statusTabs"
	    :key="item.value"
	    :label="item.label"
	    :name="item.value"
	  />
	</el-tabs>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="店员昵称" align="center" prop="nickname" />
	  <el-table-column align="center" label="店员头像" prop="avatar" width="80px">
	    <template #default="scope">
		  <el-image
		    fit="cover"
		    :src="scope.row.avatar"
		    class="flex-none w-50px h-50px"
		    @click="imagePreview(scope.row.avatar)"
		  />
	    </template>
	  </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
		  <template #default="scope">
		    <dict-tag :type="DICT_TYPE.MEMBER_SEX" :value="scope.row.sex" />
		  </template>
	  </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="微信" align="center" prop="weixin" width="160px" />
      <el-table-column label="手机号" align="center" prop="mobile" width="160px" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="相关经验" align="center" prop="experience" />
	  <el-table-column label="录音" align="center"  prop="sound" width="350px">
	  	<template #default="scope">
	  		<audio v-if="scope.row.sound!=''&&scope.row.sound!=null" :src="scope.row.sound" type="audio/wav"
	  			controls="controls"></audio>
	  		<span v-else>暂无语音</span>
	  	</template>
	  </el-table-column>
      <el-table-column label="音频时长" align="center" prop="soundTime" />
      <el-table-column label="审核状态" align="center" prop="status">
		  <template #default="scope">
		    <el-switch
		      v-model="scope.row.status"
		      :active-value="1"
		      :inactive-value="0"
		      @change="handleStatusChange(scope.row)"
		    />
		  </template>
	  </el-table-column>
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
	  <el-table-column
	    label="更新时间"
	    align="center"
	    prop="updateTime"
	    :formatter="dateFormatter"
	    width="180px"
	  />
      <el-table-column label="操作" align="center" min-width="180px" fixed="right">
        <template #default="scope">
		  <el-button
		   link
		   type="primary"
		   @click="openPriceForm('update', scope.row.id)"
		   v-hasPermi="['worker:clerk-apply:update']"
		  >
		    技能管理
		  </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['worker:clerk-apply:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['worker:clerk-apply:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ClerkApplyForm ref="formRef" @success="getList" />
  <ClerkApplyGoodsForm ref="clerkApplyGoodsFormRef" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions, getDictOptions } from '@/utils/dict'
import { TabsPaneContext } from 'element-plus'
import { createImageViewer } from '@/components/ImageViewer'
import { dateFormatter } from '@/utils/formatTime'
import { CommonStatusEnum } from '@/utils/constants'
import { cloneDeep } from 'lodash-es'
import download from '@/utils/download'
import { ClerkApplyApi, ClerkApplyVO } from '@/api/worker/clerkapply'
import ClerkApplyForm from './ClerkApplyForm.vue'
import ClerkApplyGoodsForm from './ClerkApplyGoodsForm.vue'

/** 店员申请 列表 */
defineOptions({ name: 'ClerkApply' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ClerkApplyVO[]>([]) // 列表的数据
const statusTabs = ref([
  {
    label: '全部',
    value: 0
  }
])
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  workerClerkId: undefined,
  userId: undefined,
  nickname: undefined,
  avatar: undefined,
  sex: undefined,
  age: undefined,
  weixin: undefined,
  mobile: undefined,
  city: undefined,
  experience: undefined,
  sound: undefined,
  soundType: undefined,
  soundTime: [],
  albums: undefined,
  intro: undefined,
  status: 0,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
	const data = cloneDeep(queryParams)
	// 处理掉全部的状态，不传就是全部
	if (data.status === 0) {
		delete data.status
	}
    const res = await ClerkApplyApi.getClerkApplyPage(data)
    list.value = res.list
    total.value = res.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 头像预览 */
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 技能设置操作 */
const clerkApplyGoodsFormRef = ref()
const openPriceForm = (type: string, id?: number) => {
  clerkApplyGoodsFormRef.value.open(type, id)
}

/** 切换 Tab */
const handleTabClick = (tab: TabsPaneContext) => { 
  queryParams.status = tab.paneName as number
  getList()
}

/** 修改用户状态 */
const handleStatusChange = async (row: ClerkApplyApi.ClerkApplyVO) => {
  try {
    // 修改状态的二次确认
    const text = row.status === CommonStatusEnum.ENABLE ? '拒绝' : '通过'
    await message.confirm('确认要"' + text + '""' + row.nickname + '"店员申请吗?')
    // 发起修改状态
    await ClerkApplyApi.updateClerkApplyStatus(row.id, row.status)
    // 刷新列表
    await getList()
  } catch {
    // 取消后，进行恢复按钮
    row.status =
      row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE : CommonStatusEnum.ENABLE
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ClerkApplyApi.deleteClerkApply(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ClerkApplyApi.exportClerkApply(queryParams)
    download.excel(data, '店员申请.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  // 设置 statuses 过滤
  for (const dict of getDictOptions(DICT_TYPE.WORKER_CLERK_APPLY_STATUS)) {
    statusTabs.value.push({
      label: dict.label,
      value: dict.value
    })
  }
})
</script>